import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/expert/plan_pack/plan_pack_history_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/view/king/items/history_label.dart';
import 'package:sports_new/widgets/base/qkd_text.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';

import '../../../entity/expert/plan_pack/plan_pack_entity.dart';
import '../../../utils/routes.dart';
import '../../../widgets/card.dart';
import '../../pick/items/avator_label.dart';

class PlanPackHistoryPage extends StatefulWidget {
  const PlanPackHistoryPage({super.key});

  @override
  State<PlanPackHistoryPage> createState() => _PlanPackHistoryPageState();
}

class _PlanPackHistoryPageState extends State<PlanPackHistoryPage> {
  final PlanPackHistoryLogic logic = Get.put(PlanPackHistoryLogic(),tag: "${Get.arguments}");

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlanPackHistoryLogic>(
      tag: "${Get.arguments}",
      builder: (logic) {
        return Scaffold(
          backgroundColor: Colours.greyF2F3F7,
          appBar: Styles.appBar(
            title: QkdText("${logic.expertId.hasValue?"专家":""}历史套餐")
          ),
          body: EasyRefresh.builder(
            controller: logic.refreshController,
            onRefresh: logic.getRefresh,
            onLoad: logic.loadMore,
            footer: Styles.refreshFooter(position: IndicatorPosition.locator),
            childBuilder: (BuildContext context, ScrollPhysics physics) =>
            CustomScrollView(
              physics: physics,
              slivers: [
                LoadingWidget<bool>.sliver(
                  physics: physics,
                  isLoading: logic.data == null,
                  data: logic.data?.length == 0,
                  child: () => SliverList.builder(
                    itemCount: logic.data?.length??0,
                    itemBuilder: (context,index) => item(logic.data![index])
                  ),
                ),
                const FooterLocator.sliver()
              ],
            )
          ),
        );
      }
    );
  }

  Widget item(PlanPackEntity entity){
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => Get.toNamed(Routes.planPackDetail,arguments: entity.packageId),
      child: Block(
        cardStyle: const CardStyle(
            padding: EdgeInsets.all(12),
            margin: EdgeInsets.fromLTRB(8, 8, 8, 0)
        ),
        child: Stack(
          alignment: AlignmentDirectional.topEnd,
          children: [
            Column(
              children: [
                AvatarLabel.record(
                  avatarUrl: entity.expertLogo,
                  name: entity.expertName,
                  id: entity.expertId
                ),
                Container(height: 8),
                QkdText(entity.title,maxLines: 2,color: Colours.grey66,fontSize: 14),
              ]
            ),
            if(entity.packageTag.hasValue)
            HistoryLabel.planPack(text: entity.packageTag??"").paddingOnly(top: 4,right: 4)
          ],
        )
      ),
    );
  }
}
